package cn.lili.controller.order;

import cn.lili.common.aop.annotation.PreventDuplicateSubmissions;
import cn.lili.common.security.AuthUser;
import cn.lili.common.security.OperationalJudgment;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.security.enums.AuthUserFieldEnum;
import cn.lili.common.security.enums.ObjectFieldEnum;
import cn.lili.common.security.enums.SceneEnums;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.PageVO;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.order.order.entity.dos.Receipt;
import cn.lili.modules.order.order.entity.dto.OrderReceiptDTO;
import cn.lili.modules.order.order.entity.dto.ReceiptSearchParams;
import cn.lili.modules.order.order.service.ReceiptService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * 发票记录接口
 *
 * @author paulG
 * @since 2020/11/17 4:34 下午
 **/
@RestController
@Api(tags = "发票记录接口")
@RequestMapping("/order/receipt")
@RequiredArgsConstructor
public class ReceiptController {

    private final ReceiptService receiptService;


    @ApiOperation(value = "获取发票分页信息")
    @GetMapping
    public ResultMessage<Page<OrderReceiptDTO>> getPage(ReceiptSearchParams searchParams, PageVO pageVO) {
        return ResultUtil.data(this.receiptService.getReceiptData(searchParams, pageVO));
    }

    @ApiOperation(value = "获取发票详情")
    @GetMapping("/{id}")
    public ResultMessage<Receipt> getDetail(@PathVariable String id) {
        AuthUser authUser = Objects.requireNonNull(UserContext.getCurrentUser());
        Receipt receipt;
        if (SceneEnums.MEMBER.equals(authUser.getScene())) {
            receipt = OperationalJudgment.judgment(receiptService.getById(id), ObjectFieldEnum.MEMBER_ID, AuthUserFieldEnum.ID);
        } else if (SceneEnums.STORE.equals(authUser.getScene())) {
            receipt = OperationalJudgment.judgment(receiptService.getById(id), ObjectFieldEnum.STORE_ID, AuthUserFieldEnum.EXTEND_ID);
        } else {
            receipt = OperationalJudgment.judgment(receiptService.getDetail(id));
        }
        return ResultUtil.data(receipt);
    }

    @PreventDuplicateSubmissions
    @ApiOperation(value = "保存发票信息")
    @PostMapping
    public ResultMessage<Receipt> save(@Valid Receipt receipt) {
        receipt.setMemberId(UserContext.getCurrentUser().getExtendId());
        return ResultUtil.data(receiptService.saveReceipt(receipt));
    }

    @ApiOperation(value = "开发票")
    @ApiImplicitParam(name = "id", value = "发票ID", required = true, dataType = "String", paramType = "path")
    @PostMapping(value = "/{id}/invoicing")
    public ResultMessage<Receipt> invoicing(@PathVariable String id) {
        return ResultUtil.data(receiptService.invoicing(id));
    }

}
